// import 'package:amap_base/amap_base.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wanchong/utils/common.dart';

class AmapPage extends StatefulWidget {
  @override
  _AmapPageState createState() => _AmapPageState();
}

class _AmapPageState extends State<AmapPage> {
  bool _rp = false;
  // var _amapLocation = AMapLocation();
  bool _permission = false;
  // AMapController _controller;
  @override
  void initState() {
    super.initState();

    ///初始化定位
    _initLocation();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
      ),
      body: Center(
        child: RaisedButton(
          child: Text('开始导航'),
          onPressed: () {
            // AMapNavi().startNavi(
            //   lat: 29.12,
            //   lon: 119.64,
            //   naviType: AMapNavi.ride,
            // );
          },
        ),
      ),
    );
  }

  Widget _map() {
    // return AMapView(
    //   onAMapViewCreated: (controller) {
    //     setState(() {
    //       _controller = controller;
    //     });
    //   },
    //   amapOptions: AMapOptions(
    //       myLocationEnabled: true,),
    // );
    Container();
  }

  @override
  void dispose() {
    // _amapLocation.stopLocate();
    super.dispose();
  }

  void _initLocation() async {
    // _amapLocation.init();
    // final options = LocationClientOptions(
    //   isOnceLocation: true,
    //   locatingWithReGeocode: true,
    // );
    if (!_rp) {
      _permission = await _request();
    }

    // if (_permission) {
    //   _amapLocation.getLocation(options).then((_) => print(_));
    // } else {
    //   alert("获取定位失败~");
    // }
  }

  Future<bool> _request() async {
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location);
    if (permission != PermissionStatus.granted) {
      var req = await PermissionHandler()
          .requestPermissions([PermissionGroup.location]);
      if (req[PermissionGroup.location] != PermissionStatus.granted) {
        return false;
      }
    }
    return true;
  }
}
